﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Ue.Qcdz.Domain.UrlRewriter;
using Ue.Framework.Web;

namespace Ue.Qcdz.Domain.ResourceManager
{
    [Serializable]
    [XmlRoot(ElementName="add")]
    public class SiteUrl : IReWrittenUrl
    {
        private IReWrittenUrl reWrittenUrl;
        private string path;
        private string pattern;
        private string vanity;

        public SiteUrl()
        {
            this.path = string.Empty;
            this.pattern = string.Empty;
            this.vanity = string.Empty;
        }

        /// <summary>
        /// key
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 页面访问Url
        /// </summary>
        [XmlAttribute("path")]
        public string Path
        {
            get { return this.path; }
            set { this.path = this.UrlHandler(value); }
        }

        /// <summary>
        /// 页面重写正则匹配表达式
        /// </summary>
        [XmlAttribute("pattern")]
        public string Pattern
        {
            get { return this.pattern; }
            set { this.pattern = this.UrlHandler(value); }
        }

        /// <summary>
        /// 页面重写实际访问的地址
        /// </summary>
        [XmlAttribute("vanity")]
        public string Vanity
        {
            get { return this.vanity; }
            set { this.vanity = this.UrlHandler(value); }
        }
        /// <summary>
        /// 构建网址页面访问网址，如果有Url重写，此功能生成的Url为重写后的网址
        /// </summary>
        /// <param name="queryValues">参数值列表</param>
        /// <returns></returns>
        public string BuildUrl(params object[] queryValues)
        {
            return this.BuildUrl(null, queryValues);
        }
        internal string BuildUrl(Func<string, string> urlHandle, params object[] queryValues)
        {
            var url = urlHandle != null
                ? urlHandle(this.Path)
                : this.Path;

            return queryValues == null
                ? url
                : string.Format(url, queryValues);
        }

        #region IReWrittenUrl

        public bool IsMatch(string path)
        {
            this.EnsureRewritternUrl();

            return  this.reWrittenUrl.IsMatch(path);
        }

        public string Convert(string path, string query)
        {
            this.EnsureRewritternUrl();

            return this.reWrittenUrl.Convert(path, query);
        }

        #endregion

        private void EnsureRewritternUrl()
        {
            if (this.reWrittenUrl == null)
            {
                this.reWrittenUrl = new ReWrittenUrl(this.Pattern, this.Vanity);
            }
        }

        /// <summary>
        /// 对url字符进处理，如一些在xml中需要转换的特殊符号等
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private string UrlHandler(string urlStr)
        {
            if (string.IsNullOrEmpty(urlStr))
                return string.Empty;

            //xml文件不支持&符号，所以使用^符号替代
            string url = urlStr.Replace("^", "&");

            //根据虚拟主机应用程序路径组合成完整url
            return UrlHelper.ResolveUrl(url);
        }
    }
}
